package modelo;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;


import entities.ComandaEntity;
import entities.MovimientoEntity;
import entities.SucursalEntity;
import hbt.AbstractHibernateManager;

public class ComandaDao extends AbstractHibernateManager {
	
	private static ComandaDao instancia = null;
	
	public static ComandaDao getInstancia() {
		if(instancia == null) {
			instancia = new ComandaDao();
		}
		return instancia;
	}
	
	public void save (ComandaEntity coman){
		Session s = null;
		try {
			s=getNewSession();
			s.beginTransaction();
			s.persist(coman);
			s.flush();
			s.getTransaction().commit();
			System.out.println("[comanda PERSISTED ON TABLE Comanda]::OK");
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally{
			closeSession(s);
		}
	}
	
	
	public void guardar (ComandaEntity coman){
		Session s = null;
		try {
			s=getNewSession();
			s.beginTransaction();
			s.save(coman);
			s.flush();
			s.getTransaction().commit();
			System.out.println("[comanda SAVED ON TABLE Comanda]::OK");
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally{
			closeSession(s);
		}
	}
	
	
	public void merge (ComandaEntity coman){
		Session s = null;
		try {
			s=getNewSession();
			s.beginTransaction();
			s.merge(coman);
			s.flush();
			s.getTransaction().commit();
			System.out.println("[comanda MERGED ON TABLE Comanda]::OK");
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally{
			closeSession(s);
		}
	}
	
	
	
	public boolean saveOrUpdate(ComandaEntity coman){
		Session s= null;
		try {
			s=getNewSession();
			s.beginTransaction();
			s.saveOrUpdate(coman);
			s.flush();
			s.getTransaction().commit();
			System.out.println("[comanda (SAVED OR UPDATED) ON TABLE comandas]::OK");
			return true;
		} catch (HibernateException e) {
			e.printStackTrace();
			return false;
		} finally {
			closeSession(s);
		}
	}
	
	
	public List<ComandaEntity> obtenerComandasPorArea(String area) {
		Session s = null;
		List<ComandaEntity> comandas = new ArrayList<ComandaEntity>();
		
		try {
			s = getNewSession();
			comandas =  s.createQuery("FROM ComandaEntity comanda " +
									"WHERE comanda.item.plato.area.tipo LIKE :area " +
									"AND comanda.estado LIKE 'AREALIZAR'")
							  .setString("area", area)
							  .list();
			
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally{
			closeSession(s);
		}
		return comandas;
	}

	
	public ComandaEntity obtenerComandasPorId(Integer id) {
		Session s = null;
		ComandaEntity comanda = null;
		try {
			s = getNewSession();
			comanda =  (ComandaEntity) s.createQuery("FROM ComandaEntity comanda " +
														"WHERE comanda.id = :id ")
							  .setInteger("id", id)
							  .uniqueResult();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally{
			closeSession(s);
		}
		return comanda;
	}
	
}
